package Controller;
import java.sql.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import Modele.*;
import Vue.CasePosBean;

public class CtrlMonde extends Thread{
	
    private CtrlAppli ctrl_appli;
    private Monde monde;
    private static List<CasePosBean> listeCases = new ArrayList<CasePosBean>();
    
    public CtrlMonde(Monde _monde,CtrlAppli _ctrl_appli){
    	super();
    	this.ctrl_appli = _ctrl_appli;
    	this.ctrl_appli.setCtrl_monde(this);
    	this.monde=_monde; 
    }

    public void vivre (Case cel)
    {
    	if(cel instanceof Fourmiliere)
    	{

    		if(((Fourmiliere) cel).getStockNourriture()==0)
    		{
    			List<Fourmi> listHome = ((Fourmiliere) cel).getListeFourmis();
    			for(int i=0;i<listHome.size();i++)
    			{
    				listHome.get(i).mourrir();
    			}
    			listHome.clear();
    			
    			Position pos = cel.getMonde().getPositionFromCase(cel);
    			Map<Position,Case> map = cel.getMonde().getCarte();
    			map.put(pos, new Case(0, 0, new ArrayList<Fourmi>(), 0, monde));
    		}
    	}
    	cel.dissiperPheromone();
    	if(cel.getFourmi()!= null)
    	{
	    	for(int i=0;i<cel.getFourmi().size();i++)
	    		cel.getFourmi().get(i).action();
    	}

    }
    
    public void ajouter(CasePosBean cpb)
    {
    	getListeCases().add(cpb);
    	monde.setCaseAtPosition(cpb.getP().getX(), cpb.getP().getY(), cpb.getC());
    	ctrl_appli.refreshFenetres();
    }
    
    public void majMap() {
		List<CasePosBean> listeCases = getListeCases();
		
			for (CasePosBean cpb : listeCases)
			{				
				this.getMonde().setCaseAtPosition(cpb.getP().getX(), cpb.getP().getY(), cpb.getC());
			}
		
	}
    
	public void reInitMovement()
	{
		for( Case cel : monde.getCarte().values())
    		if(cel.getFourmi()!=null)
    			for(Fourmi fourmi : cel.getFourmi())
    				fourmi.setMoved(false);
	}

    public void setCarte(int width,int height){
    	
    }
    
    public void run() {
    	
/*    	int nbrEclaireuse=0;
    	int nbrOuvriere=0;
    	int nbrNourriture=0;
    	int nbrPheromone=0;
    	String nomFourm="";*/
    	int nbrPheromone = 0;
    	reInitMovement();
    	for( Case cel : monde.getCarte().values())
    	{
    		vivre(cel);
    		if(cel instanceof Fourmiliere)
    		{
    			int nbrEclaireuse = ((Fourmiliere) cel).getNbEclaireuses();
    			int nbrOuvriere = ((Fourmiliere) cel).getNbOuvrieres();
    			int nbrNourriture = ((Fourmiliere) cel).getStockNourriture();
    			String nomFourm = ((Fourmiliere)cel).getName();
    			
    			
    			Connection c = null;
    			Statement stmt = null;
    		    try {
    		      Class.forName("org.sqlite.JDBC");
    		      c = DriverManager.getConnection("jdbc:sqlite:test.db");
    		      c.setAutoCommit(false);
    		      System.out.println("Opened database successfully");

    		      stmt = c.createStatement();
    		      String sql = "INSERT INTO STATS_FOURMI (NBR_ECLAIR,NBR_OUVR,NOURR_STOCK,NOM_FOURM)" +
    		                   "VALUES ("+nbrEclaireuse+","+nbrOuvriere+","+nbrNourriture+",'"+nomFourm+"');"; 
    		      stmt.executeUpdate(sql);
    		      stmt.close();
    		      c.commit();
    		      c.close();
    		      System.out.println("insert database successfully");
    		    } catch ( Exception e ) {
    		      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
    		      System.exit(0);
    		    }
	    	}
    		nbrPheromone = nbrPheromone + cel.getNiveauPheromone();
    	}
    	Connection c = null;
		Statement stmt = null;
	    try {
	      Class.forName("org.sqlite.JDBC");
	      c = DriverManager.getConnection("jdbc:sqlite:test.db");
	      c.setAutoCommit(false);
	      System.out.println("Opened database successfully");

	      stmt = c.createStatement();
	      String sql = "INSERT INTO PHERO_MAP (NBR_PHERO)" +
	                   "VALUES ("+nbrPheromone+");"; 
	      stmt.executeUpdate(sql);
	      stmt.close();
	      c.commit();
	      c.close();
	      System.out.println("insert database phero successfully");
	    } catch ( Exception e ) {
	      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
	      System.exit(0);
	    }
    	
    	Random r = new Random();
		if(r.nextInt(8)==0)
		{
			int xRand = r.nextInt(monde.getLargeur());
			int yRand = r.nextInt(monde.getHauteur());
			monde.getCaseFromPosition(new Position(xRand,yRand)).ajouterRandomNourriture(750);
		}
    	
    	this.ctrl_appli.refreshFenetres();

    }

	public static List<CasePosBean> getListeCases() {
		return listeCases;
	}

	public static void setListeCases(List<CasePosBean> listeCases) {
		CtrlMonde.listeCases = listeCases;
	}

	public Monde getMonde() {
		return monde;
	}

	public void setMonde(Monde monde) {
		this.monde = monde;
	}
}
